<?php
/**
 *  Phpwell Google Login Test
 *
 *  Note: update the load_google_login method below to load the Google
 *  Account login and password with which you will test.
 *
 *  Based on Greqo library: http://code.google.com/p/greqo/
 *
 *  References:
 *  http://code.google.com/apis/base/faq_gdata.html#clientlogin
 *  http://code.google.com/apis/books/docs/gdata/developers_guide_protocol.html
 */

/**
 * Imports
 */
$phpwell_dir = dirname(dirname(dirname(dirname(__FILE__))));
require_once(sprintf('%s/%s', $phpwell_dir, 'import.php'));
import_well('test/functional');
import_well('web/client');


/**
 * Test Class
 */
class GoogleLoginFunctionalCase extends PhpwellFunctionalTestCase {

    var $client_id          = 'Phpwell-Google-Login';
    var $google_service     = 'print';  # Google Books API
    var $google_login_url   = 'https://www.google.com/accounts/ClientLogin';
    var $google_user        = '';
    var $google_pass        = '';

    var $Browser = NULL;

    function startAll() {
        $this->Browser = new HttpClient();
        $this->Browser->error_on_unexpected_code = 0;

        $Conf = import_config('conf/google.php');
        $this->google_user = $Conf['username'];
        $this->google_pass = $Conf['password'];
    }

    function endAll() {
        $this->Browser = NULL;
    }

    function parseAuthTokens($content) {
        $TokenDict = array();
        $Lines = explode("\n", trim($content));
        foreach ( $Lines as $line ) {
            $Pair = explode('=', trim($line));
            $TokenDict[$Pair[0]] = $Pair[1];
        }
        return $TokenDict;
    }


    /**
     * Tests
     */
    function testInstance() {
        $this->assert($this->is_instance($this->Browser, 'HttpClient'));
    }

    function testLoadGoogleUser() {
        $this->assert(! empty($this->google_user));
        $this->assert(! empty($this->google_pass));
        #$this->log(array($this->google_user, $this->google_pass));
    }

    function testRequestClientLoginToken()
    {
        $auth_token = '';

        # set headers
        $HeaderDict = array(
            'Content-type' => 'application/x-www-form-urlencoded',
        );

        # set POST values
        $PostDict = array(
            'accountType'   => 'GOOGLE',
            'Email'         => $this->google_user,
            'Passwd'        => $this->google_pass,
            'service'       => $this->google_service,
            'source'        => $this->client_id,
        );

        # make request
        $content = $this->Browser->post($this->google_login_url, $PostDict,
                                        $HeaderDict);
        $this->assert($this->Browser->Response->status == 200);


        # parse tokens
        $TokenDict = $this->parseAuthTokens($content);
        $this->assert(isset($TokenDict['Auth']));

        # logs
        $this->log($TokenDict);
        #$this->log($TokenDict['Auth']);
        #$this->log($this->Browser->ResponseList);
    }

}

$Test = &new GoogleLoginFunctionalCase();
$Test->run();
print $Test->report();
